    
    
    const Koa = require('koa');
    const KoaRouter = require('koa-router');
    // koa-bodyparser模块用于存储页面发来的请求数据 存储格式为json对象
    const bodyParser = require('koa-bodyparser');
    const koaNunjucks = require('koa-nunjucks-2');
    const path = require('path');

    const app = new Koa();
    const router = new KoaRouter();

    app.use(bodyParser());
    app.use(koaNunjucks({
        ext: 'html',
        path: path.join(__dirname, 'views'),
        nunjucksConfig: {
            trimBlocks: true
        }
    }));
    app.use(router.routes());
    app.use(router.allowedMethods());

    app.listen('3000', (err) => {
        if(err) console.log("koa 服务器启动失败");
        else console.log("koa 服务器启动成功，地址为http://localhost:3000");
    });

    router.get('/', (ctx, next) => {
        ctx.body = "<h1>Hello Koa Server</h1>";
    });

    router.get('/login', async (ctx, next) => {
        await ctx.render('login', {});
    });

    router.post('/checklogin', async (ctx, next) => {
        // 使用ctx.request.body方法获取数据（koa-bodyparser）
        const {username,password} = ctx.request.body;
        console.log(ctx.request.body);
        console.log(username + '----' + password);
        if(checkUser(users, username, password)) {
            await ctx.render('home', {
                user: username,
            });
        }
        else {
            await ctx.render('login', {
                info: '账号密码错误'
            });
        }
    });

    